-- CallSummon
-- Created by cheny3 Dec/21/2016
-- 召唤兽召唤

-- 初始化
function initCallSummon(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    local offsetY = tonumber(buildingInfo["icon_offset_y"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:setPositionY(self.icon:getPositionY() + offsetY);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    if self.gridData.need_animation == 1 then
        -- 呼吸
        self:doModelBreath(self.icon);
    end

    local function okFunc()
        -- 召唤
        local pos = self.gridData:getPos();
        if DungeonActionM.go("call_summon", pos) then
            alert(buildingInfo["bonus_tip"]);
        end
    end

    local function cancelFunc()
        -- 不召唤
        if type(buildingInfo["fail_tip"]) == "string" and string.len(buildingInfo["fail_tip"]) > 0 then
            alert(buildingInfo["fail_tip"]);
        end
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 打开建筑查看界面
            self:openBuildingView2(okFunc, cancelFunc, buildingInfo["viewText"], buildingInfo["viewText2"]);
        end
    end
    self.icon:addTouchEventListener(onClicked);
end
